<?php

class MallSpecAttrsController extends Controller
{

    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $specs_id = Util::getParamNotEmpty("specs_id");
            $attrs_id = Util::getParamNotEmpty("attrs_id");
            $sort = Util::getParam("sort","1");


            //check

            $spec_obj = MallSpecs::model()->findByPK($specs_id);
            Util::checkExistAndNotDelete($spec_obj);

            $attr_obj = MallAttrs::model()->findByPK($attrs_id);
            Util::checkExistAndNotDelete($attr_obj);


            $obj = MallSpecAttrsService::getObj($specs_id,$attrs_id);

            if(empty($obj)==true)
            {
                //添加
                $obj = new MallSpecsAttrs();
                Util::initObjData($obj);

            }
            else
            {
                if($obj->is_delete == 1)
                {
                    Util::recoverObjData($obj);
                }
                else
                {
                    throw new Exception("已存在该关系");
                }
            }

            $obj->specs_id = $specs_id;
            $obj->attrs_id = $attrs_id;
            $obj->attrs_name = $attr_obj->name;
            $obj->sort = $sort;



            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }


    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();
            $specs_id = Util::getParamNotEmpty("specs_id");


            $searchsql = Util::initSearchSql();
            $searchsql = Util::getSerarchStrAdd($searchsql,"specs_id",$specs_id);


            //获取count
            $totalcount = MallSpecsAttrs::model()->count($searchsql);

                //排序
                $searchsql = $searchsql . " order by create_time desc";

                //加上分页控制
                $searchsql = Util::addLimitSql($searchsql);

                //获取list
                $list =MallSpecsAttrs ::model()->findAll($searchsql);


                Util::responseList($totalcount, $list);

            } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }


    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            $specs_id = Util::getParamNotEmpty("specs_id");
            $attrs_id = Util::getParamNotEmpty("attrs_id");




            //参数验证


            $obj = MallSpecAttrsService::getObj($specs_id,$attrs_id);

            if(empty($obj)==true)
            {
                throw new Exception("不存在该关系");
            }

            if($obj->is_delete == 1)
                throw new Exception("关系已删除");

            Util::deleteObjData($obj);
            Util::saveObjData($obj);
                    //返回数据
                    Util::responseMsg(MsgConst::RETURN_SUCCESS);


                } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }
}